package com.llmm.eduService.service.impl;

import com.llmm.eduService.entity.EduTeacher;
import com.llmm.eduService.mapper.EduTeacherMapper;
import com.llmm.eduService.service.EduTeacherService;
import com.llmm.exception.CommonErrorCode;
import com.llmm.exception.ExceptCase;
import com.llmm.utils.RandomUtil;
import com.llmm.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class EduTeacherServiceImpl implements EduTeacherService {

    @Resource
    private EduTeacherMapper teacherMapper;

    //条件分页查询所有教师信息
    @Override
    public List<EduTeacher> queryTeachers(Map<String,Object> map) {
        log.info("queryTeachers param is :{}",map);
        if (map.get("page")!=null ||map.get("size")!=null){
            Integer page = Integer.parseInt(map.get("page").toString());
            Integer size = Integer.parseInt(map.get("size").toString());
            map.put("size",size);
            map.put("offset",(page-1)*size);
        }
        List<EduTeacher> eduTeachers = teacherMapper.queryTeachers(map);
        return eduTeachers;
    }

    //查询教师总数
    @Override
    public Integer countTeachers(Map<String,Object> map) {
        log.info("countTeachers param is : {}",map);
        Integer integer = teacherMapper.countTeachers(map);
        return integer;
    }

    @Override
    public EduTeacher queryTeacherById(String id) {
        if (StringUtil.isEmpty(id)){
            log.error("id is missing");
            ExceptCase.cast(CommonErrorCode.E_100002);
        }
        log.info("queryTeacherById id is : {}",id);
        EduTeacher eduTeacher = teacherMapper.queryTeacherById(id);
        return eduTeacher;
    }

    @Override
    public Integer deleteById(String ids) {
        if (StringUtil.isEmpty(ids)){
            log.error("id is missing");
            ExceptCase.cast(CommonErrorCode.E_100002);
        }
        log.info("deleteById map is : {}",ids);
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        Integer integer = teacherMapper.deleteById(strings);
        return integer;
    }

    @Override
    public Integer saveTeacher(EduTeacher teacher) {
        log.info("saveTeacher param is : {}",teacher);
        teacher.setId(RandomUtil.getSixBitRandom()+RandomUtil.getFourBitRandom());
        teacher.setGmtCreate(new Date());
        teacher.setIsDeleted(0);
        Integer integer = teacherMapper.saveTeacher(teacher);
        return integer;
    }

    @Override
    public Integer updateTeacher(EduTeacher teacher) {
        log.info("updateTeacher param is : {}",teacher);
        teacher.setGmtModified(new Date());
        Integer integer = teacherMapper.updateTeacher(teacher);
        return integer;
    }
}
